package com.monkeygroup.mygoplus.ecchat.ui.contact.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.DefinedView.CircleImageView;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;

import java.util.List;

/**
 * Created by Administrator on 2017/6/24.
 */
public class ChooseMemberAdatper extends BaseAdapter {
    private List<ContactMemberDTO> data = null;
    private Context mContext;
    private LayoutInflater inflater;
    private OnChooseItemClickListener myListener = null;

    public ChooseMemberAdatper(Context context, List<ContactMemberDTO> data, OnChooseItemClickListener listener) {
        this.mContext = context;
        this.data = data;
        this.myListener = listener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (data != null) {
            return data.size();
        }

        return 0;
    }

    @Override
    public ContactMemberDTO getItem(int position) {
        if (data != null && data.size() > 0) {
            return data.get(position);
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_invite_contact, null);
            holder.choose_state = (ImageView) convertView.findViewById(R.id.choose_state);
            holder.member_name = (TextView) convertView.findViewById(R.id.member_name);
            holder.member_head = (CircleImageView) convertView.findViewById(R.id.member_head);
            holder.choose_state.setTag(0);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ContactMemberDTO memberDTO = getItem(position);
        holder.member_name.setText(memberDTO.getName());
        if (holder.choose_state.getTag().equals(0)) {
            holder.choose_state.setImageResource(R.drawable.btn_setting_chosebox);
        } else {
            holder.choose_state.setImageResource(R.drawable.btn_setting_chosebox_press);
        }

        holder.choose_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag().equals(0)) {
                    v.setTag(1);
                    if (myListener != null) {
                        myListener.onChooseClick(position, true);
                    }
                } else {
                    v.setTag(0);
                    if (myListener != null) {
                        myListener.onChooseClick(position, false);
                    }
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        ImageView choose_state;
        CircleImageView member_head;
        TextView member_name;
    }

    public interface OnChooseItemClickListener {
        void onChooseClick(int position, boolean flag);
    }
}